//
//  SettingLoginPassword.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/30.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "SettingLoginPassword.h"

@interface SettingLoginPassword ()

@end

@implementation SettingLoginPassword

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"修改登录密码"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    
    _submitBtn.layer.masksToBounds = YES;
    _submitBtn.layer.cornerRadius = BTN_RADIUS;
    _submitBtn.userInteractionEnabled = NO;
    _submitBtn.backgroundColor = UNUSEABLECOLOR;
    
    _oldPwd.keyboardType = UIKeyboardTypeASCIICapable;
    _recentPwd.keyboardType = UIKeyboardTypeASCIICapable;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)visibleClick:(id)sender {
    if (_recentPwd.secureTextEntry) {
        [_visibleBtn setBackgroundImage:[UIImage imageNamed:@"显示"] forState:UIControlStateNormal];
    } else {
        [_visibleBtn setBackgroundImage:[UIImage imageNamed:@"不显示"] forState:UIControlStateNormal];
    }
    _recentPwd.secureTextEntry = !_recentPwd.secureTextEntry;
}

- (IBAction)submitClick:(id)sender {
    if (![self judgePassWordLegal:_recentPwd.text]) {
        JSSHOW(@"密码要设为6-20位数字、字母组合。");
        return;
    }
    DZSHOWHUD;
    DZEditPwdRequest *request = [[DZEditPwdRequest alloc] init];
    request.pwd = _oldPwd.text;
    request.npwd = _recentPwd.text;
    [DZNetAPI request:request success:^(id response) {
        DZHIDEHUD;
        if ([response[@"rt"] integerValue] == 1) {
            JSSHOW(@"登录密码修改成功");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            
            JSSHOW(response[@"mes"]);
        }
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
        DZHIDEHUD;
    }];
}

- (IBAction)oldPwdChanged:(id)sender {
    if (_oldPwd.text.length != 0 && _recentPwd.text.length != 0) {
        _submitBtn.backgroundColor = BLUE_COLOR;
        _submitBtn.userInteractionEnabled = YES;
    } else {
        _submitBtn.backgroundColor = UNUSEABLECOLOR;
        _submitBtn.userInteractionEnabled = NO;
    }
}

- (IBAction)recentPwdChanged:(id)sender {
    if (_oldPwd.text.length != 0 && _recentPwd.text.length != 0) {
        _submitBtn.backgroundColor = BLUE_COLOR;
        _submitBtn.userInteractionEnabled = YES;
    } else {
        _submitBtn.backgroundColor = UNUSEABLECOLOR;
        _submitBtn.userInteractionEnabled = NO;
    }
}

/**
 * 功能： 判断长度大于6位小于20位并是否同时包含且只有数字和字母
 */
- (BOOL)judgePassWordLegal:(NSString *)text{
    
    BOOL result = false;
    if ([text length] >= 6){
        
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:text];
    }
    return result;
}
@end
